#ifndef USERINFO_ADAPTER_H_
#define USERINFO_ADAPTER_H_
#include "base/config_reader.h"
#include "UserService.h"
#include "melon/client/thrift_client_pool.h"
#include "melon/client/zk_service_locator.h"
namespace user{

class UserAdapter{
public:
	static UserAdapter& Instance(){
		static UserAdapter instance;
		return instance;
	}	
        std::string GetServerKey() {
          return std::string("/im/userservice");
        }
	
    void getUserBasicInfo(UserBaseInfo& _return, const int64_t user_id) {
      UserServiceClient* client = pool_.Alloc(GetServerKey());
      if (client == NULL) {
	return;
      }
      bool success = true;
      try {
        client->getUserBasicInfo(_return, user_id);
        LOG_INFO("UserAdapter  getUserBasicInfo ok. uid=" << user_id);
      } catch (const std::exception & e) {
        LOG_WARN("UserAdapter getUserBasicInfo err. uid=" << user_id << " e=" << e.what());
        success = false;
      }
      pool_.Release(GetServerKey(), client, success);
    }

    void getUserToken(std::string& _return, const int64_t user_id) {
      UserServiceClient* client = pool_.Alloc(GetServerKey());
      if (client == NULL) {
        return;
      }

      bool success = true;
      try {
	client->getUserToken(_return, user_id);
        LOG_INFO("UserAdapter getUserToken ok. uid=" << user_id);
      } catch (const std::exception & e) {
        LOG_WARN("UserAdapter getUserToken err. uid=" << user_id << " e=" << e.what());
        success = false;
      }
      pool_.Release(GetServerKey(), client, success);
    }

  
    bool setUserBasicInfo(const UserBaseInfo& info) {
	bool result = false;
	UserServiceClient * client = pool_.Create(GetServerKey());
        result = client->setUserBasicInfo(info);
	pool_.Release(GetServerKey(), client, true);
	return result;
    }
  
    int createUserEducation(const UserEducateInfo& info) {
	UserServiceClient * client = pool_.Create(GetServerKey());
	int result = client->createUserEducation(info);
	pool_.Release(GetServerKey(), client, true);
	return result;
    }
    bool updateUserEducation(int item_id, const UserEducateInfo& info) {
	UserServiceClient * client = pool_.Create(GetServerKey());
	bool result = client->updateUserEducation(item_id, info);
	pool_.Release(GetServerKey(), client, true);
	return result;
    }
    void getUserLastEdu(UserEducateInfo& _return, const int64_t user_id) {
	UserServiceClient * client = pool_.Create(GetServerKey());
	client->getUserLastEdu(_return, user_id);
	pool_.Release(GetServerKey(), client, true);
    }
  
    void getUserEduList(std::vector<UserEducateInfo> & _return, const int64_t user_id) {
	UserServiceClient * client = pool_.Create(GetServerKey());
	client->getUserEduList(_return, user_id);
	pool_.Release(GetServerKey(), client, true);
    }
  
    void getUserWorkLastExp(UserWorkExpInfo& _return, const int64_t user_id) {
	UserServiceClient * client = pool_.Create(GetServerKey());
	client->getUserWorkLastExp(_return, user_id);
	pool_.Release(GetServerKey(), client, true);
    }
  
    void getUserWorkExpList(std::vector<UserWorkExpInfo> & _return, const int64_t user_id) {
	UserServiceClient * client = pool_.Create(GetServerKey());
	client->getUserWorkExpList(_return, user_id);
	pool_.Release(GetServerKey(), client, true);
    }

    int createUserWorkExpirence(const UserWorkExpInfo& info) {
	UserServiceClient * client = pool_.Create(GetServerKey());
	int result = client->createUserWorkExpirence(info);
	pool_.Release(GetServerKey(), client, true);
	return result;
    }

    bool updateUserWorkExpirence(int item_id, const UserWorkExpInfo& info) {
	UserServiceClient * client = pool_.Create(GetServerKey());
	bool result = client->updateUserWorkExpirence(item_id, info);
	pool_.Release(GetServerKey(), client, true);
	return result;
    }
	
private:
      UserAdapter() : pool_() {
        ConfigReader zk_cfg("../conf/zookeeper.conf");
        if (!zk_cfg) {
          LOG_WARN("Config file ../conf/zookeeper.conf read error!");
          return;
        }
        pool_.set_locator(new melon::client::ZkServiceLocator(zk_cfg.Get("zookeeper", "service_registry").c_str()));
      }

      melon::client::ClientPool<UserServiceClient> pool_;
};
}


#endif
